package com.ki.rent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ki.rent.entity.House;
import com.ki.rent.entity.User;
import com.ki.rent.entity.UserHouse;
import com.ki.rent.service.BookingService;
import com.ki.rent.service.HostService;
import com.ki.rent.service.HouseService;
import com.ki.rent.service.UserHouseService;
import com.ki.rent.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 */
@Controller
@RequestMapping("/rent/house")
@Transactional
public class HouseController {

    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private HostService hostService;
    @Autowired
    private UserHouseService userHouseService;
    @Autowired
    private BookingService bookingService;

    //添加房源
    @RequestMapping("/addHouse")
    public String toAddHouse(Model model, HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        if (userId == null){
            userId = 0;
        }
        User user = userService.getById(userId);
        model.addAttribute("user",user);
        model.addAttribute("userId",userId);
        return "house/addhouse";
    }

    //我发布的房源
    @RequestMapping("/myHouse/{userId}")
    public String myHouse(@PathVariable("userId") Integer userId,Model model){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("host_id",userId);
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses", houses);
        return "house/myhouse";
    }

    //到更新房源信息页面
    @RequestMapping("/toUpdateHouse/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        return "house/updatehouse";
    }

    //更新房源成功页面
    @RequestMapping("/updateHouse")
    @ResponseBody
    public String updateHouse(House house) {
        boolean b = houseService.saveOrUpdate(house);
        if(b) {
            return "OK";
        }
        return "FAIL";
    }

    //查看现有未租出房源
    @RequestMapping("/allHouse")
    public String allHouse(Model model){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_still",0);
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        int size = houses.size();
        model.addAttribute("size",size);
        return "house/allhouse";
    }

    //价格降序
    @RequestMapping("/houseDesc")
    public String HouseDesc(Model model){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_still",0);
        wrapper.orderByDesc("house_price");
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        int size = houses.size();
        model.addAttribute("size", size);
        return "house/allhouse";
    }

    //价格升序
    @RequestMapping("/houseAsc")
    public String HouseAsc(Model model, HttpServletRequest request){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_still",0);
        wrapper.orderByAsc("house_price");
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        int size = houses.size();
        model.addAttribute("size", size);
        return "house/allhouse";
    }

    //点击查看房源
    @RequestMapping("/seeHouse/{houseId}")
    public String house(@PathVariable("houseId") Integer houseId, Model model, HttpSession session){
        House house = houseService.getById(houseId);
        User user = userService.getById(house. getHostId());
        String username = user.getUsername();
        model.addAttribute("username",username);
        model.addAttribute("house",house);
        session.setAttribute("houseId",houseId);
        String houseDetail = house.getHouseDetail();
        if (houseDetail != null && "" != houseDetail){
            String[] split = houseDetail.split("~");
            if (split.length >= 3){
                String img1 = split[0];
                String img2 = split[1];
                String img3 = split[2];
                model.addAttribute("img1",img1);
                model.addAttribute("img2",img2);
                model.addAttribute("img3",img3);
            }else if (split.length == 2){
                String img1 = split[0];
                String img2 = split[1];
                model.addAttribute("img1",img1);
                model.addAttribute("img2",img2);
            }else{
                String img1 = split[0];
                model.addAttribute("img1",img1);
            }
        }
        return "house/showhouse";
    }


    @RequestMapping("/addOrder")
    @ResponseBody
    public String addOrder( Model model, HttpServletRequest request){
        Integer houseId = (Integer) request.getSession().getAttribute("houseId");
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_id",houseId);
        wrapper.eq("house_still",0);
        System.out.println(houseId);
        House house = houseService.getOne(wrapper);
        if (house != null){
            return "OK";
        }else {
            model.addAttribute("msg","该房可能已经租出去了哦");
            return "FAIL";
        }
    }

    //到预约页面
    @RequestMapping("/toGetHouse")
    public String getHouse(Model model, HttpServletRequest request){
        Integer houseId = (Integer) request.getSession().getAttribute("houseId");
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        model.addAttribute("houseId",houseId);
        model.addAttribute("userId",userId);
        return "house/gethouse";
    }

    //为租出房源
    @RequestMapping("/inHouse")
    public String inHouse(Model model, HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_still",0);
        wrapper.eq("host_id",userId);
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        return "house/inhouse";
    }

    //已租出房源
    @RequestMapping("/outHouse")
    public String outHouse(Model model, HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_still",1);
        wrapper.eq("host_id",userId);
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        return "house/outhouse";
    }


    //成功完成预约
    @RequestMapping("/success/id")
    public String houseStill(Integer houseId, Integer userId, Integer bookingId, Model model){
        House house = houseService.getById(houseId);
        house.setHouseStill(1);
        house.setUserId(userId);
        houseService.saveOrUpdate(house);
        bookingService.removeById(bookingId);
        UserHouse userHouse = new UserHouse();
        userHouse.setUserId(userId);
        userHouse.setHouseId(houseId);
        userHouse.setStill(1);
        userHouseService.save(userHouse);
        return "redirect:/rent/booking/hostBooking";
    }

    //恢复已出租的房子为未出租
    @RequestMapping("/backHouse/{houseId}")
    public String backHouse(@PathVariable("houseId") Integer houseId, Model model){
        QueryWrapper<UserHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("house_id",houseId);
        House house = houseService.getById(houseId);
        house.setUserId(0);
        house.setHouseStill(0);
        UserHouse userHouse = userHouseService.getOne(wrapper);
        userHouse.setStill(0);
        userHouseService.saveOrUpdate(userHouse);
        boolean b = houseService.saveOrUpdate(house);
        if (b){
            return "redirect:/rent/house/outHouse";
        }
        model.addAttribute("msg","退房失败");
        return "redirect:/rent/house/outHouse";
    }

    //查看自己租的房
    @RequestMapping("/myRent")
    public String myRent(Model model, HttpSession session){
        Integer userId = (Integer) session.getAttribute("userId");
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("house_still",1);
        List<House> houses = houseService.list(wrapper);
        int size = 0;
        if (houses != null){
            size = houses.size();
        }
        model.addAttribute("size",size);
        model.addAttribute("houses",houses);
        return "house/myStillHouse";
    }

    //退房
    @RequestMapping("/return")
    public String returnHouse(Integer houseId,Model model){
        QueryWrapper<UserHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("house_id",houseId);
        UserHouse userHouse = userHouseService.getOne(wrapper);
        userHouse.setStill(0);
        return "redirect:/rent/house/myRent";
    }

    //根据条件找房子
    @RequestMapping("/findHouse")
    public String findHouse(String keywords, Model model){
        List<House> houses = houseService.findHouse(keywords);
        int size = houses.size();
        model.addAttribute("size",size);
        model.addAttribute("houses", houses);
        return "house/allhouse";
    }


}
